﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Pixiv.Pixivision
{
    internal class PixivisionHelper
    {
        public static void DownloadClass(string Url)
        {
            string Shtml = WebHelper.HttpPcGet(Url);
            foreach (string Line in HtmlHelper.GetLinkVlaueList(Shtml, "//h2[@class='arc__title ']/a"))
            {
                Console.WriteLine(Line);
                DownloadSingle("https://www.pixivision.net" + Line);
            }
        }

        public static void AutoStart(string Url)
        {
            //https://www.pixivision.net/zh/a/3341
            //https://www.pixivision.net/zh/c/manga
            if (Url.IndexOf("zh/a") > 0)
                DownloadSingle(Url);
            else
                DownloadClass(Url);
        }

        /// <summary>
        /// 下载单个链接
        /// </summary>
        /// <param name="Url"></param>
        internal static void DownloadSingle(string Url)
        {
            string Shtml = WebHelper.HttpPcGet(Url);

            string FileName = string.Format("图集ID（{0}）", FileNameHelper.GetFileType(Url)) + HtmlHelper.GetFirstText(Shtml, "//h1[@class='am__title ']")
               + HtmlHelper.GetFirstText(Shtml, "//p[@class='_date am__sub-info__date large light-gray']")
               + AnysisTag(Shtml);

            string FloderPath = StaticValueOld.DownloadPath + TextHelper.ReplaceBadChar(FileName) + "\\";
            Console.WriteLine("FileName" + FloderPath);
            FloderHelper.FolderCreate(FloderPath);

            String Content = string.Format("标题：\r\n{0}\r\n信息：\r\n{1}", FileName, AnysisImgInfo(Shtml));

            FileHelper.WriteText(FloderPath + "文章信息.txt", Content);
            List<string> ImgList = HtmlHelper.GetImgSrcVlaueList(Shtml, "//img[@class='am__work__illust  ']");
            foreach (string Line in ImgList)
            {
                string ImgLink = Line.Replace("c/768x1200_80/img-master", "img-original").Replace("_master1200", "");
                Console.WriteLine(ImgLink);
                ImgLink = PixivHelper.GetImg(ImgLink.Substring(0, ImgLink.Length - 4));
                Console.WriteLine("获取到" + ImgLink);
               WebHelper.DownloadToFloder(ImgLink, FloderPath, new PixivHelper().PixivHeader(ImgLink));
                //https://i.pximg.net/c/768x1200_80/img-master/img/2015/11/29/19/22/17/53802717_p0.jpg
                //https://i.pximg.net/img-original/img/2017/05/18/01/00/35/62948640_p0.jpg
                //https://i.pximg.net/img-original/img/2017/10/01/07/20/52/65215529_p0.jpg
            }
        }

        internal static void BatchDownload(string StartPage, string EndPage)
        {
            int StartPageCount = TextHelper.StringToInt(StartPage);
            int EndPageCount = TextHelper.StringToInt(EndPage);
            for (; StartPageCount > 0 && StartPageCount < EndPageCount;)
            {
                DownloadSingle("https://www.pixivision.net/zh/a/" + StartPageCount++);
            }
        }

        /// <summary>
        /// 分析获取tag
        /// </summary>
        /// <param name="Shtml"></param>
        /// <returns></returns>
        private static string AnysisTag(string Shtml)
        {
            string Temptext = "";
            foreach (string line in HtmlHelper.GetTextValueList(Shtml, "//li[@class='tls__list-item button']"))
            {
                Console.WriteLine(line);
                Temptext += line.Trim() + "、";
            }
            return string.Format("标签({0})", Temptext);
        }

        private static string AnysisImgInfo(string Shtml)
        {
            String Content = HtmlHelper.GetFirstText(Shtml, "//div[@class='am__description _medium-editor-text']");
            string ArticleBody = HtmlHelper.GetFirstHtml(Shtml, "//div[@class='am__body']");
            string SplitText = "\r\n---------------------------\r\n";
            //var result = node.SelectNodes(".//span[contains(@class,'expire')]");
            foreach (string Line in HtmlHelper.GetHtmlValueList(ArticleBody, "//div[contains(@class,'am__work gtm__illust-collection-illusts')]"))
            {
                string Temptext = SplitText + "图片标题：" +
                     HtmlHelper.GetFirstText(Line, "//a[@class='inner-link']") + "\r\n"
                     + HtmlHelper.GetFirstLink(Line, "//a[@class='inner-link']").Replace("&amp;", "&") + "\r\n"
                     + "画师信息:"
                     + HtmlHelper.GetFirstText(Line, "//a[@class='author-img-container inner-link']") + "\r\n"
                     + HtmlHelper.GetFirstLink(Line, "//a[@class='author-img-container inner-link']") + "\r\n";
                int GetComicCount = HtmlHelper.GetHtmlValueList(Line, "//div[@class='_multipage-image-container with-piled-effect']").Count;
                if (GetComicCount > 0)
                    Temptext = Temptext.Replace("图片标题", "图片（多P）标题");
                int GetGifComicCount = HtmlHelper.GetHtmlValueList(Line, "//div[@class='_ugoira-player-container']").Count;
                if (GetGifComicCount > 0)
                    Temptext = Temptext.Replace("图片标题", "图片（Gif）标题");

                Content += Temptext;

                Console.WriteLine(Temptext);
            }
            return Content;
        }
    }
}